from django.db.models import Q
from drf_yasg2.utils import swagger_auto_schema
from rest_framework import viewsets
from drf_yasg2 import openapi
from rest_framework.decorators import action

from PAA_ERP.utils.base_views import MultipleOpenMixin, MultipleDestroyMixin
from PAA_ERP.utils.pagination import GlobalPagination
from basic_info.models import WarehouseModel
from basic_info.serializer.warehouse_serializer import WarehouseSerializer, WarehouseSearchhSerializer


class WarehouseView(viewsets.ModelViewSet, MultipleOpenMixin, MultipleDestroyMixin):
    """
    create:
    仓库信息--新增

    仓库信息新增, status: 201(成功), return: 新增仓库信息信息

    destroy:
    仓库信息--删除

    仓库信息删除, status: 204(成功), return: None

    multiple_delete:
    仓库信息--批量删除,必传参数：ids=[1,2,3,4...]

    仓库信息批量删除, status: 204(成功), return: None

    multiple_open:
    仓库信息--批量启用或者禁用,必传(json)参数：ids=[1,2,3,4...](列表中可以只有一个)，is_open=1/0
    {
        "ids":[1,2],
        "is_open":"0"
    }
    is_open=1表示禁用，is_open=0表示启用，
    仓库信息批量启用或者禁用, status: 204(成功), return: None

    update:
    仓库信息--修改,

    仓库信息修改, status: 200(成功), return: 修改后的仓库信息信息

    partial_update:
    仓库信息--局部修改,可以传参任意属性的值，服务器会修改指定的属性值 仓库信息局部修改,

    status: 200(成功), return: 修改后的仓库信息信息

    list:
    仓库信息--获取分页列表，可选json参数:name(名称)，mobile(手机号码)，phone(联系电话)
    {
        "name":"长沙",
        "mobile":"186"
    }
    仓库信息列表信息, status: 200(成功), return: 仓库信息信息列表

    retrieve:
    查询某一个仓库信息

    查询指定ID的仓库信息, status: 200(成功), return: 用户仓库信息
    """
    queryset = WarehouseModel.objects.all()
    serializer_class = WarehouseSerializer
    pagination_class = GlobalPagination

    def get_serializer_class(self):
        if self.action == 'find':
            return WarehouseSearchhSerializer
        return WarehouseSerializer

    def get_queryset(self):
        if self.action == "find":
            # if self.action == "list":
            # 接受三个可能的参数
            name = self.request.data.get("name", None)
            remark = self.request.data.get("remark", None)
            # 应为参数可能没有，造成过滤条件是动态的
            query = Q()
            if name:
                # 模糊查询 name like 'keyword%
                query.add(Q(name__contains=name), "AND")  # 多条件组合
            if remark:
                query.add(Q(remark__contains=remark), "AND")  # 多条件组合

            return WarehouseModel.objects.filter(query).all()
        else:
            return WarehouseModel.objects.all()

    json_params = openapi.Schema(type=openapi.TYPE_OBJECT, properties={
        'name': openapi.Schema(type=openapi.TYPE_STRING, description='仓库名字关键字'),
        'remark': openapi.Schema(type=openapi.TYPE_STRING, description='仓库描述关键字'),
    })
    # 在文档中输入页号页数的接口
    page_param = openapi.Parameter(name='page', in_=openapi.IN_QUERY, description='页号', type=openapi.TYPE_STRING)
    size_param = openapi.Parameter(name='size', in_=openapi.IN_QUERY, description='每页数量', type=openapi.TYPE_STRING)

    @swagger_auto_schema(method='post', request_body=json_params, manual_parameters=[page_param, size_param],
                         operation_description='仓库过滤查询')
    @action(methods=['post'], detail=False)
    def find(self, request, *args, **kwargs):
        return super(WarehouseView, self).list(request=request, *args, **kwargs)
